package com.android.tripspins.app.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.android.tripspins.app.bo.Contact;

/**
 * Created by joaortk on 02/01/14.
 */
public class ContactDAO {

    private SQLiteDatabase database;
    public ContactDAO(SQLiteDatabase database) {
        this.database = database;
    }
    public void persist(long attachmentId, Contact contact)
    {
        if(getContact(attachmentId)==null)
            insertContact(attachmentId, contact);
        else updateContact(attachmentId);
    }

    private void updateContact(long attachmentId) {
        StringBuilder sb = new StringBuilder();
        sb.append(" Update Contacts set txt_synced = 'N' ");
        sb.append( " Where id_attachment = ?");
        database.execSQL(sb.toString(),new String[]{
                String.valueOf(attachmentId)});
    }

    public void insertContact(long attachmentId, Contact contact)
    {
        StringBuilder sbInsert = new StringBuilder();
        sbInsert.append("INSERT INTO contacts ( id_attachment ");
        sbInsert.append(                        ", txt_source ");
        sbInsert.append(                        ", txt_id_on_source ");
        sbInsert.append(                        ", txt_name ");
        sbInsert.append(                        ", txt_picture_path ");
        sbInsert.append(                        ", txt_thumbnail_path ");
        sbInsert.append(                        ", txt_phone_number )");
        sbInsert.append(                 "VALUES ( ? ");//id_attachment
        sbInsert.append(                        ", ? ");//txt_source
        sbInsert.append(                        ", ? ");//txt_id_on_source
        sbInsert.append(                        ", ? ");//txt_name
        sbInsert.append(                        ", ? ");//txt_picture_path
        sbInsert.append(                        ", ? ");//txt_thumbnail_path
        sbInsert.append(                        ", ? );");//txt_phone_number

        database.execSQL(sbInsert.toString(),new String[]{
                String.valueOf(attachmentId),
                contact.getContactSource(),
                contact.getContactIdOnSource(),
                contact.getName(),
                contact.getPicturePath(),
                contact.getPictureThumbnailPath(),
                contact.getPhoneNumber()});
    }

    public Contact getContact(long attachmentId)  {

        Contact contact = null;
        StringBuilder sb = new StringBuilder();
        sb.append("Select txt_source ");  //0
        sb.append(     ", txt_id_on_source ");   //1
        sb.append(     ", txt_name ");    //2
        sb.append(     ", txt_picture_path ");     //3
        sb.append(     ", txt_thumbnail_path ");   //4
        sb.append(     ", txt_phone_number ");  //5
        sb.append( " From contacts");
        sb.append( " Where id_attachment = ?");

        Cursor cursorContacts = database.rawQuery(
                sb.toString(),
                new String[] { String.valueOf(attachmentId) });
        cursorContacts.moveToFirst();
        if (!cursorContacts.isAfterLast()){
            contact = new Contact();
            contact.setContactSource(cursorContacts.getString(0));
            contact.setContactIdOnSource(cursorContacts.getString(1));
            contact.setName(cursorContacts.getString(2));
            contact.setPicturePath(cursorContacts.getString(3));
            contact.setPictureThumbnailPath(cursorContacts.getString(4));
            contact.setPhoneNumber(cursorContacts.getString(5));

        }
        cursorContacts.close();
        return contact;



    }


}
